package com.si.sea_island.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.si.sea_island.pojo.UserDB;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class AutomaticLogonUtil {

    public static void automaticLogon(String phone,String directUserPhone,String password,String payPassword){
        //自动注册科创互联
        automaticLogonKCHL(phone,directUserPhone,password,payPassword);
//        //自动注册海岛
//        automaticLogonSI(phone,directUserPhone,password,payPassword);
        //自动注册积分世界
        automaticLogonIW(phone,directUserPhone,password,payPassword);

    }

    /**
     * 自动注册科创互联
     * @param phone
     */
    public static void automaticLogonKCHL(String phone,String directUserPhone,String password,String payPassword){
        try {
            //线上地址
//            URL url = new URL("http://www.longshijieapp.com/kchl/api/automaticLogon?"
            //本地测试地址
            URL url = new URL("http://127.0.0.1:8010/api/automaticLogon?"
                    +"phone="+phone
                    +"&directUserPhone="+directUserPhone
                    +"&password="+password
                    +"&payPassword="+payPassword
                    +"&privateKey="+"kchl20241212E9HoNFu1nFtmks8GKy47o4mexXc2wl4vDZ2GPwbcQbLTAAOW8");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json; utf-8");
            connection.setRequestProperty("Accept", "application/json");
            connection.setDoOutput(true);

            int responseCode = connection.getResponseCode();

            UserDB user = new UserDB();

            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                // 打印响应体
                System.out.println("Response Body: " + response.toString());
//                //获取data
//                JsonObject jsonObject = JsonParser.parseString(response.toString()).getAsJsonObject();
//                String data = jsonObject.get("data").toString();
//                //解析成userDB
//                Gson gson = new Gson();
//                user = gson.fromJson(data, UserDB.class);
            }

            System.out.println("POST Response Code :: " + responseCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 自动注册海岛
     * @param phone
     */
    public static void automaticLogonSI(String phone,String directUserPhone,String password,String payPassword){
        try {
            //线上地址
//            URL url = new URL("http://www.longshijieapp.com/SI/api/automaticLogon?"
            //本地测试地址
            URL url = new URL("http://127.0.0.1:8020/api/automaticLogon?"
                    +"phone="+phone
                    +"&directUserPhone="+directUserPhone
                    +"&password="+password
                    +"&payPassword="+payPassword
                    +"&privateKey="+"SI20241212E9Hogjce55ftmk124GKy47o4mdsxXc2wl4FxaGPwbAQbLTqweW8");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json; utf-8");
            connection.setRequestProperty("Accept", "application/json");
            connection.setDoOutput(true);

            int responseCode = connection.getResponseCode();

            UserDB user = new UserDB();

            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                // 打印响应体
                System.out.println("Response Body: " + response.toString());
//                //获取data
//                JsonObject jsonObject = JsonParser.parseString(response.toString()).getAsJsonObject();
//                String data = jsonObject.get("data").toString();
//                //解析成userDB
//                Gson gson = new Gson();
//                user = gson.fromJson(data, UserDB.class);
            }

            System.out.println("POST Response Code :: " + responseCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }






    /**
     * 自动注册积分世界
     * @param phone
     */
    public static void automaticLogonIW(String phone,String directUserPhone,String password,String payPassword){
        try {
            //线上地址
//            URL url = new URL("http://www.longshijieapp.com/IW/api/automaticLogon?"
            //本地测试地址
            URL url = new URL("http://127.0.0.1:8030/api/automaticLogon?"
                    +"phone="+phone
                    +"&directUserPhone="+directUserPhone
                    +"&password="+password
                    +"&payPassword="+payPassword
                    +"&privateKey="+"IW20241212saaSogjceqtre5g3skx2ks2iSbo4mdsxXc2Sl4FxafPwbAaLTqwddg8");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json; utf-8");
            connection.setRequestProperty("Accept", "application/json");
            connection.setDoOutput(true);

            int responseCode = connection.getResponseCode();

            UserDB user = new UserDB();

            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                // 打印响应体
                System.out.println("Response Body: " + response.toString());
//                //获取data
//                JsonObject jsonObject = JsonParser.parseString(response.toString()).getAsJsonObject();
//                String data = jsonObject.get("data").toString();
//                //解析成userDB
//                Gson gson = new Gson();
//                user = gson.fromJson(data, UserDB.class);
            }

            System.out.println("POST Response Code :: " + responseCode);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




}
